<template>
  <el-dialog
    :visible.sync="dialog"
    :loading="loading"
    :close-on-click-modal="false"
    :title="titleMap[dialogStatus]"
    width="75%"
    left
    @close="closeDlg"
  >
    <div class="app-container">
      <div style="padding-top:20px">
        <el-form ref="dataInfo" :model="dataInfo" :inline="true" :rules="rules" size="small" label-position="right" label-width="200px">
          <el-form-item label="原产地代码:" prop="originCode">
            <el-input v-model="dataInfo.originCode" style="width: 200px;" class="filter-item" />
          </el-form-item>
          <el-form-item label="原产地英文名:">
            <el-input v-model="dataInfo.originEn" style="width: 200px;" class="filter-item" />
          </el-form-item>
          <el-form-item label="原产地中文名:">
            <el-input v-model="dataInfo.originCn" style="width: 200px;" class="filter-item" />
          </el-form-item>
          <el-form-item label="OFFICIAL NAME:">
            <el-input v-model="dataInfo.officialName" style="width: 200px;" class="filter-item" />
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-row :gutter="20">
      <el-col :span="1" :offset="8"><div class="grid-content bg-purple"><el-button type="primary" size="small" @click="onSubmit(dataInfo)">提交</el-button></div></el-col>
      <el-col :span="1" :offset="4"><div class="grid-content bg-purple"><el-button type="primary" size="small" @click="closeDlg">取消</el-button></div></el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { Message } from 'element-ui'
import { submitCounTryOfOriginInfo } from '@/api/plugs'

export default {
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      dialog: false,
      dataInfo: {
      },
      loading: false,
      titleMap: {
        addMap: '新增',
        editMap: '编辑'
      },
      dialogStatus: '',
      rules: { // 表单验证规则
        originCode: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    closeDlg() {
      this.dialog = false
    },
    // 提交触发
    onSubmit(ruleInfo) {
      this.$refs.dataInfo.validate((valid) => {
        if (valid) {
          if (ruleInfo) {
            this.loading = true
            this.$confirm('是否确认保存?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              submitCounTryOfOriginInfo(this.dataInfo).then(() => {
                this.loading = false
                this.parent.toQuery()
                Message({
                  message: '保存成功。',
                  type: 'success',
                  duration: 3 * 1000
                })
                this.dialog = false
              })
            })
          } else {
            Message({
              message: 'Dept+Fineline 必填',
              type: 'warning',
              duration: 3 * 1000
            })
            return false
          }
        }
      })
    }
  }
}
</script>

